par(mfrow = c(2,2))
set.seed(1)
rm(list = ls())
library(grf)
library(haven)
data <- read_dta("C:/Users/Desktop/数据.dta")
Y <- as.matrix(data[,3])
W <- as.matrix(data[,6])
X <- as.matrix(data[,c(7:18)])
Y.forest <- regression_forest(X, Y, equalize.cluster.weights = TRUE)
Y.hat <- predict(Y.forest)$predictions
W.forest <- regression_forest(X, W, equalize.cluster.weights = TRUE)
W.hat <- predict(W.forest)$predictions
varimp <- variable_importance(Y.forest)
selected.idx <- which(varimp / mean(varimp)>0.2)
X2 <- X[,selected.idx]
tau.forest1 <- causal_forest(X2, Y, W,
                   Y.hat = Y.hat, W.hat = W.hat,
                   num.trees = 500,
                   equalize.cluster.weights = TRUE,
                   tune.parameters = "all")
tau.hat.oob1 <- predict(tau.forest1)
hist(tau.hat.oob1$predictions,xlab="(a)树的数量为500时，条件平均处置效应",ylab="频数",main="",family = "myFont")


set.seed(1)
rm(list = ls())
library(grf)
library(haven)
data <- read_dta("C:/Users/Desktop/数据.dta")
Y <- as.matrix(data[,3])
W <- as.matrix(data[,6])
X <- as.matrix(data[,c(7:18)])
Y.forest <- regression_forest(X, Y, equalize.cluster.weights = TRUE)
Y.hat <- predict(Y.forest)$predictions
W.forest <- regression_forest(X, W, equalize.cluster.weights = TRUE)
W.hat <- predict(W.forest)$predictions
varimp <- variable_importance(Y.forest)
selected.idx <- which(varimp / mean(varimp)>0.2)
X2 <- X[,selected.idx]
tau.forest2 <- causal_forest(X2, Y, W,
                             Y.hat = Y.hat, W.hat = W.hat,
                             num.trees = 2000,
                             equalize.cluster.weights = TRUE,
                             tune.parameters = "all")
tau.hat.oob2 <- predict(tau.forest2)
hist(tau.hat.oob2$predictions,xlab="(b)树的数量为2000时，条件平均处置效应",ylab="频数",main="",family = "myFont")


set.seed(1)
rm(list = ls())
library(grf)
library(haven)
data <- read_dta("C:/Users/Desktop/数据.dta")
Y <- as.matrix(data[,3])
W <- as.matrix(data[,6])
X <- as.matrix(data[,c(7:18)])
Y.forest <- regression_forest(X, Y, equalize.cluster.weights = TRUE)
Y.hat <- predict(Y.forest)$predictions
W.forest <- regression_forest(X, W, equalize.cluster.weights = TRUE)
W.hat <- predict(W.forest)$predictions
varimp <- variable_importance(Y.forest)
selected.idx <- which(varimp / mean(varimp)>0.2)
X2 <- X[,selected.idx]
tau.forest3 <- causal_forest(X2, Y, W,
                             Y.hat = Y.hat, W.hat = W.hat,
                             num.trees = 4000,
                             equalize.cluster.weights = TRUE,
                             tune.parameters = "all")
tau.hat.oob3 <- predict(tau.forest3)
hist(tau.hat.oob3$predictions,xlab="(C)树的数量为4000时，条件平均处置效应",ylab="频数",main="",family = "myFont")


set.seed(1)
rm(list = ls())
library(grf)
library(haven)
data <- read_dta("C:/Users/Desktop/数据.dta")
Y <- as.matrix(data[,3])
W <- as.matrix(data[,6])
X <- as.matrix(data[,c(7:18)])
Y.forest <- regression_forest(X, Y, equalize.cluster.weights = TRUE)
Y.hat <- predict(Y.forest)$predictions
W.forest <- regression_forest(X, W, equalize.cluster.weights = TRUE)
W.hat <- predict(W.forest)$predictions
varimp <- variable_importance(Y.forest)
selected.idx <- which(varimp / mean(varimp)>0.2)
X2 <- X[,selected.idx]
tau.forest4 <- causal_forest(X2, Y, W,
                             Y.hat = Y.hat, W.hat = W.hat,
                             num.trees = 8000,
                             equalize.cluster.weights = TRUE,
                             tune.parameters = "all")
tau.hat.oob4 <- predict(tau.forest4)
hist(tau.hat.oob4$predictions,freq = F,xlab="(d)树的数量为8000时，条件平均处置效应",ylab="频数",main="",family = "myFont")



